<?php
/**
 * Created by PhpStorm.
 * User: Duck
 * Create time: 2016/12/21 9:56
 */

namespace D\Controller;
use Common\Controller\BaseController;

class PointsController extends BaseController{
    public function show(){
        $roleName=M('points_role')->where(array('lock'=>'0'))->select();
        $rN='';
        foreach($roleName as $v){
            $rN.=$v['points_roles_id'].',';
        }
        $rN=substr($rN,0,-1);
        if($_SESSION['type']=='teachers') {
            $class=M('teachers')->find($_SESSION['id']);
            $stu = M('students')->where(array('class' => $class['tclass']))->select();
            foreach ($stu as $k => $v) {
                $where = [
                    'student_id' => $v['student_id'],
                    'points_time' => date('Y/M/d', time()),
                ];
                $rid = M('points')->where($where)->find();
                $a = explode(',', $rid['points_roles_id']);
                $b = explode(',', $rid['points_frequency']);
                $numm = 0;
                $stu[$k]['rp'] = array();
                foreach ($roleName as $j => $l) {
                    if (in_array($l['points_roles_id'], $a)) {
                        $stu[$k]['rp'][$j] = '<span style="float:left;cursor:pointer" onclick="minus(\'' . $v['student_id'] . '_' . $l['points_roles_id'] . '\')">-</span><span class="b_' . $v['student_id'] . '_' . $l['points_roles_id'] . ' d_' . $v['student_id'] . '" style="float:left">' . $b[$numm] . '</span><span style="float:left;cursor:pointer" onclick="plus(\'' . $v['student_id'] . '_' . $l['points_roles_id'] . '\')">+</span>';
                        $numm++;
                    } else {
                        $stu[$k]['rp'][$j] = '<span style="float:left;cursor:pointer" onclick="minus(\'' . $v['student_id'] . '_' . $l['points_roles_id'] . '\')">-</span><span style="float:left" class="b_' . $v['student_id'] . '_' . $l['points_roles_id'] . '">0</span><span style="float:left;cursor:pointer" onclick="plus(\'' . $v['student_id'] . '_' . $l['points_roles_id'] . '\')">+</span>';
                    }
                }
            }
            $this->assign('stu', $stu);
        }
        $this->assign('roleName', $roleName);
        $this->assign('rN',$rN);
        $this->display();
    }
    public function save(){
        $_POST['points_time']=date('Y/M/d',time());
        $_POST['mtime']=time();
        $b= I('post.points_roles_id');
        $a=M('points_role')->select($b);
        $pf=explode(',',$_POST['points_frequency']);
        $num=0;
        foreach($a as $k=>$v){
            $num=$num+$v['val']*$pf[$k];
        }
        $_POST['score']=$num;
        if(D('points')->create()){
            $where=[
                'student_id'=>I('post.student_id'),
                'points_time'=>I('post.points_time'),
            ];
            if(M('points')->where($where)->find()){
                M('points')->where($where)->delete();
            }
            if(D('points')->add()){
                $res='保存成功';
            }else{
                $res='保存失败';
            }
        }else{
            $res='数据存在问题';
        }
        $this->ajaxReturn($res);
    }
}